package cn.wolfcode.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-11-08 9:14
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse=(HttpServletResponse) servletResponse;
        Object user_in_session = httpServletRequest.getSession().getAttribute("USER_IN_SESSION");

        if(user_in_session==null){
            System.out.println("-----------------没有访问权限----------------");
            /**
             *
             */
//            httpServletRequest.setAttribute("loginMsg","没有登录，不能访问");
//            httpServletRequest.getRequestDispatcher("/login.jsp").forward(httpServletRequest,httpServletResponse);
            httpServletResponse.sendRedirect("/login.jsp");
            return;
        }

        //放行
        filterChain.doFilter(httpServletRequest,httpServletResponse);


    }

    @Override
    public void destroy() {
    }
}
